package com.example.smartshopper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String url = "http://my-smart-shop.tk/scripts/getAllProductsByBarcodeJSON.php?s=";
	private TextView name_product, code_product, category_product,
			description_product, country_product, date_product;
	private String barcode;
	JSONObject jsonobject;
	JSONArray jsonarray;
	ProgressDialog productProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.barcodeButton).setOnClickListener(scanBarCode);
		findViewById(R.id.qrcodeButton).setOnClickListener(scanQRCode);
		name_product = (TextView) findViewById(R.id.nameProduct);
		code_product = (TextView) findViewById(R.id.codeProduct);
		category_product = (TextView) findViewById(R.id.categoryProduct);
		description_product = (TextView) findViewById(R.id.descriptionProduct);
		country_product = (TextView) findViewById(R.id.countryProduct);
		date_product = (TextView) findViewById(R.id.dateProduct);

	}

	private final Button.OnClickListener scanBarCode = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			IntentIntegrator integrator = new IntentIntegrator(
					MainActivity.this);
			integrator.addExtra("SCAN_WIDTH", 800);
			integrator.addExtra("SCAN_HEIGHT", 200);
			integrator.addExtra("RESULT_DISPLAY_DURATION_MS", 3000L);
			integrator.addExtra("PROMPT_MESSAGE",
					"Custom prompt to scan a product");
			integrator.initiateScan(IntentIntegrator.PRODUCT_CODE_TYPES);
		}
	};

	private final Button.OnClickListener scanQRCode = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			IntentIntegrator integrator = new IntentIntegrator(
					MainActivity.this);
			integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES);
		}
	};

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		IntentResult result = IntentIntegrator.parseActivityResult(requestCode,
				resultCode, intent);
		if (result != null) {
			barcode = result.getContents().toString();
			String contents = result.getContents();
			if (contents != null) {

				new getProduct().execute();

			} else {
				Toast.makeText(MainActivity.this, "Product not found", 15000)
						.show();

			}

		}
	}

	private class getProduct extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			productProgress = new ProgressDialog(MainActivity.this);
			productProgress.setMessage("Please Wait ... ");
			productProgress.setIndeterminate(false);
			productProgress.setCancelable(false);
			productProgress.show();

		}

		@Override
		protected Void doInBackground(Void... params) {
			// Create an array
			// Retrieve JSON Objects from the given URL address
			jsonobject = JSONfunctions.getJSONfromURL(url + barcode);

			try {
				// Locate the array name in JSON
				jsonarray = jsonobject.getJSONArray("Products");
				jsonobject = jsonarray.getJSONObject(0);

			} catch (JSONException e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void args) {
			super.onPostExecute(args);
			productProgress.dismiss();
			try {
				name_product.setText(jsonobject.getString("name_product"));
				code_product.setText(jsonobject.getString("code_product"));
				category_product.setText(jsonobject
						.getString("category_product"));
				description_product.setText(jsonobject
						.getString("description_product"));
				country_product
						.setText(jsonobject.getString("country_product"));
				date_product.setText(jsonobject.getString("date_product"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
